<template>
    <div class="view">
        <android-nav-bar title="现金积分明细" v-on:back="back"></android-nav-bar>
        <div class="bill-box">
            <div class="none-bill" v-if="!bill.length">
                暂无明细!
            </div>
            <cube-scroll class="scroll bills" ref="scroll" :scroll-events="['scroll']"
                         :data="bill"
                         :options="options"
                         @pulling-down="onPullingDown"
                         @pulling-up="onPullingUp"
                         v-if="bill.length">
                <div class="bill" v-for="item in bill">
                    <div class="money-time">
                        <span class="money" :class="item.money.indexOf('-')!==-1?'reduce':'add'">{{item.money.indexOf('-')!==-1?'':'+'}}{{item.money}}</span>
                        <span class="time rt">{{item.create_time|timeStamp}}</span>
                    </div>
                    <div class="desc">{{item.desc}}</div>
                </div>
            </cube-scroll>
        </div>

    </div>
</template>

<script lang="ts">
    import {Component, Vue, Watch} from "vue-property-decorator";
    import Scene from "@/render/Scene";
    import Http from "@/service/Http";

    import {Getter, Action, State, Mutation} from "vuex-class";
    import {InterfaceUser} from "@/interface/InterfaceUser";
    import CommonUnit from "@/unit/CommonUnit";
    import {updateUser} from "@/store/dispatches";
    import UserEntity from "@/entity/UserEntity";
    import AndroidNavBar from "@/components/aui/AndroidNavBar.vue";

    @Component({
        components: {
            AndroidNavBar
        }
    })
    export default class MineMoneyBill extends Scene {
        private publicPath: string = process.env.BASE_URL;
        private bill: any[] = [];
        private page: number = 1;
        @State("UserStore")
        public user!: InterfaceUser;
        private get options(): any {
            return {
                pullDownRefresh: {txt: "更新成功"},
                pullUpLoad: {
                    threshold: 0,
                    txt: {more: "", noMore: "没有更多数据了..."},
                    visible: true
                },
                scrollbar: true
            };
        }

        private onPullingDown(): void {
            (this as any).$refs.scroll.forceUpdate();
        }

        private onPullingUp(): void {
            Http.get(this.api.moneyDetail, {page: this.page, size: 10}, {token: this.user.token}).then((res) => {
                if (res.code == 1) {
                    if (res.data.data !== []) {
                        this.bill = this.bill.concat(res.data.data);
                        this.$nextTick(() => {
                            (this as any).$refs.scroll.refresh();
                            (this as any).$refs.scroll.forceUpdate();
                        });
                        this.page++;
                    } else if (res.data.data == []) {
                        (this as any).$refs.scroll.forceUpdate();
                    }
                }
            });
        }

        private back(): void {
            this.navigateBack();
        }

        private toPage(name: string, query: any = {}): any {
            this.navigateTo({
                name: name,
                query: query
            });
        }

        private getBill(): void {
            let loadToast = (<any> this).$createToast({
                time: 0,
                type: "loading",
                mask: true,
                txt: "加载中..."
            }).show();
            Http.get(this.api.moneyDetail, {page: this.page, size: 10}, {token: this.user.token}).then((res) => {
                if (res.code == 1) {
                    loadToast.hide();
                    this.page++;
                    this.bill=res.data.data;
                    console.log(res);
                } else {
                    loadToast = (<any> this).$createToast({
                        time: 2500,
                        type: "error",
                        mask: true,
                        txt: `${res.msg}`,
                        onTimeout: () => {
                            this.navigateBack();
                        }
                    }).show();
                }
            }).catch((res) => {
                loadToast.hide();
                console.log(res);
                (<any> this).$createToast({
                    time: 2500,
                    type: "error",
                    txt: "网络繁忙,暂无信息!",
                    mask: true,
                    onTimeout: () => {
                        this.navigateBack();
                    }
                }).show();
            });
        }

        private mounted(): void {
            this.getBill();
        }

    }
</script>

<style scoped lang="stylus">
    .view
        position relative;
        width 100vw;
        height 100vh;
        background $theme-page-common-background-color;
        overflow hidden;

        .bill-box
            margin-top calc(var(--safe-area-inset-top) + 47px);
            height calc(100vh - var(--safe-area-inset-top) - 47px);

            &::before
                display table;
                content '';

            .none-bill
                height 100px;
                line-height 100px;
                text-align center;
                color $theme-viceColor-font;

            .bills
                background-color #fff;
                padding-bottom var(--safe-area-inset-bottom);
                .bill
                    padding 15px;
                    border-bottom 1px solid $theme-line-color;
                    .money-time
                        padding-bottom 15px;
                        .money
                            font-size 18px;
                            font-weight 600;
                        .money.add
                            color red;
                        .money.reduce
                            color green;
                        .time
                            color $theme-viceColor-font;
                    .desc
                        line-height 1.5em;

</style>
